package com.yf.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.yf.entity.CmsCategory;
import com.yf.entity.CmsContent;
import com.yf.entity.SysUser;
import com.yf.service.ArticleService;
import com.yf.service.CategoryService;

@Controller
@RequestMapping("/admin/article")
public class AdminArticleController {
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private ArticleService articleService;

	/**
	 * 打 开添加文章页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	private String articleAdd(Model model) {
		List<CmsCategory> categoryList = categoryService.selectAll();
		model.addAttribute("categoryList", categoryList);
		return "CMS-article-add";
	}

	/**
	 * 提交文章
	 * 
	 * @param req
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/submit", method = RequestMethod.POST)
	private String addArticle(HttpServletRequest req) {
		String title = req.getParameter("title");
		String content = req.getParameter("content");
		Integer categoryId = Integer.parseInt(req.getParameter("department"));
		SysUser user = (SysUser) req.getSession().getAttribute("user");
		Map<String, Object> addArticleMap = new HashMap<>();
		addArticleMap.put("title", title);
		addArticleMap.put("content", content);
		addArticleMap.put("user", user);
		addArticleMap.put("departmentId", categoryId);
		articleService.addArticle(addArticleMap);
		return "redirect:/admin/article/list/" + categoryId;
	}

	/**
	 * 分部门查询文章
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/list/{id}")
	private ModelAndView ArticleList(@RequestParam(value = "pn", defaultValue = "1") Integer pn,
			@RequestParam(value = "pz", defaultValue = "10") Integer pz, 
			@PathVariable Integer id) {
		ModelAndView mv = new ModelAndView();
		PageInfo<?> pageInfo=articleService.getArticle(id, pn, pz);
		CmsCategory department=categoryService.getCategoryById(id);
		mv.addObject("pageInfo"+id, pageInfo);
		mv.addObject("department", department);
		mv.setViewName("CMS-article-list");
		return mv;
	}

	/**
	 * 编辑文章
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	private ModelAndView aticleEdit(@PathVariable Long id, 
			@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
		ModelAndView mv =new ModelAndView();
		CmsContent content=articleService.getArticleById(id);
		CmsCategory department=categoryService.getCategoryById(content.getCategoryId());
		mv.addObject("content", content);
		mv.addObject("department", department);
		mv.addObject("pn", pn);
		mv.setViewName("CMS-article-edit");
		return mv;
	}

	/**
	 * 更新文章
	 * 
	 * @param id
	 * @param pn
	 * @param req
	 * @return
	 */
	@RequestMapping("/update/{id}")
	private String aticleUpdate(@PathVariable Long id, 
			@RequestParam(value = "pn", defaultValue = "1") Integer pn,
			HttpServletRequest req) {
		String title = req.getParameter("title");
		String content = req.getParameter("content");
		Integer categoryId = Integer.parseInt(req.getParameter("department"));
		SysUser user = (SysUser) req.getSession().getAttribute("user");
		CmsContent article = new CmsContent();
		article.setId(id);
		article.setTitle(title);
		article.setContent(content);
		article.setCategoryId(categoryId);
		article.setUserId(user.getId());
		article.setUpdateDate(new Date());
		articleService.updateArticle(article);
		return "redirect:/admin/article/list/" + categoryId + "?pn=" + pn;
	}
}
